package com.tengju.motivation.infrastructure.repository.activity;

import com.tengju.motivation.domain.model.performance.MotivationActivityConfig;
import com.tengju.motivation.domain.model.performance.MotivationActivityConfigId;
import com.tengju.motivation.domain.model.performance.MotivationActivityConfigRepository;
import com.tengju.motivation.infrastructure.persistence.mybatis.dataobject.MotivationActivityConfigDO;
import com.tengju.motivation.infrastructure.persistence.mybatis.mapper.MotivationActivityConfigMapper;
import com.tengju.motivation.infrastructure.repository.activity.convert.MotivationActivityConfigConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: 李大仙
 * @date: 2021/7/22 3:07 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MotivationActivityConfigRepositoryImpl implements MotivationActivityConfigRepository {

    private final MotivationActivityConfigMapper motivationActivityConfigMapper;

    @Override
    public int add(MotivationActivityConfig motivationActivityConfig) {
        MotivationActivityConfigDO motivationActivityConfigDO = MotivationActivityConfigConvert.convert(motivationActivityConfig);
        return motivationActivityConfigMapper.insertSelective(motivationActivityConfigDO);
    }

    @Override
    public int getTotal(String title) {
        return motivationActivityConfigMapper.getTotal(title);
    }

    @Override
    public List<MotivationActivityConfig> queryByTitlePage(String title, Integer offset, Integer size) {
        List<MotivationActivityConfigDO> motivationActivityConfigDOList = motivationActivityConfigMapper.selectByTitlePage(title, offset, size);
        return motivationActivityConfigDOList.stream()
                .map(MotivationActivityConfigConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public MotivationActivityConfig getByActivityId(MotivationActivityConfigId motivationActivityConfigId) {
        MotivationActivityConfigDO motivationActivityConfigDO = motivationActivityConfigMapper.selectById(motivationActivityConfigId.getValue());
        return MotivationActivityConfigConvert.convert(motivationActivityConfigDO);
    }

    @Override
    public int save(MotivationActivityConfig motivationActivityConfig) {
        MotivationActivityConfigDO convert = MotivationActivityConfigConvert.convert(motivationActivityConfig);
        return motivationActivityConfigMapper.insertSelective(convert);
    }

    @Override
    public int edit(MotivationActivityConfig motivationActivityConfig) {
        MotivationActivityConfigDO convert = MotivationActivityConfigConvert.convert(motivationActivityConfig);
        return motivationActivityConfigMapper.updateSelective(convert);
    }

    @Override
    public int remove(MotivationActivityConfigId activityId) {
        return motivationActivityConfigMapper.batchDelete(List.of(activityId.getValue()));
    }
}
